package com.seecen.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seecen.pojo.User;
import com.seecen.service.UserService;
import com.seecen.mapper.UserMapper;
import com.seecen.utils.JwtHelper;
import com.seecen.utils.MD5Util;
import com.seecen.utils.Result;
import com.seecen.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
* @author W
* @description 针对表【t_user(用户信息)】的数据库操作Service实现
* @createDate 2025-07-21 13:00:00
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result getUserAll() {
        List<User> userList = userMapper.selectList(null);
        return Result.ok(userList);
    }

    @Autowired
    private JwtHelper jwtHelper;

    @Override
    public Result login(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,user.getUsername());
        User loginUser = userMapper.selectOne(queryWrapper);
        //
        if (loginUser==null){
            return Result.build(null, ResultCodeEnum.USERNAME_ERROR);
        }

        if (!StringUtils.isEmpty(user.getPassword())
                && MD5Util.encrypt(user.getPassword()).equals(loginUser.getPassword())){
            String token = jwtHelper.createToken(Long.valueOf(loginUser.getUserId()));

            HashMap map = new HashMap();
            map.put("token",token);

            return Result.ok(map);
        }


        return Result.build(null,ResultCodeEnum.PASSWORD_ERROR);
    }

    @Override
    public Result register(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, user.getUsername());
        Long count = userMapper.selectCount(queryWrapper);
        if (count > 0) {
            return Result.build(null, ResultCodeEnum.USERNAME_USED);
        }

        user.setPassword((MD5Util.encrypt(user.getPassword())));
        userMapper.insert(user);
        return Result.ok(null);
    }

    @Override
    public Result getUserInfo(String token) {
        boolean isExpiration = jwtHelper.isExpiration(token);
        if (isExpiration){
            return Result.build(null,ResultCodeEnum.NOTLOGIN);
        }

        int userId = jwtHelper.getUserId(token).intValue();

        User user = userMapper.selectById(userId);

        user.setPassword("");

        HashMap map = new HashMap();

        map.put("loginUser",user);

        return Result.ok(map);

    }

    @Override
    public Result checkUserName(String username) {

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username);
        Long count = userMapper.selectCount(wrapper);

        if (count == 0){
            return Result.ok(null);
        }

        return Result.build(null,ResultCodeEnum.USERNAME_USED);
    }

    @Override
    public Result resetPassword(User user) {
        // 1. 检查用户名是否为空
        if (StringUtils.isEmpty(user.getUsername())) {
            return Result.build(null, ResultCodeEnum.USERNAME_ERROR);
        }

        // 2. 检查新密码是否为空
        if (StringUtils.isEmpty(user.getPassword())) {
            return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
        }

        // 3. 检查用户是否存在
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, user.getUsername());
        User existingUser = userMapper.selectOne(wrapper);

        if (existingUser == null) {
            return Result.build(null, ResultCodeEnum.USERNAME_ERROR);
        }

        // 4. 检查新密码是否与旧密码相同
        String encryptedNewPwd = MD5Util.encrypt(user.getPassword());
        if (encryptedNewPwd.equals(existingUser.getPassword())) {
            return Result.build(null, ResultCodeEnum.PASSWORD_UNCHANGED);
        }

        // 5. 更新密码
        existingUser.setPassword(encryptedNewPwd);
        int result = userMapper.updateById(existingUser);

        if (result > 0) {
            return Result.ok(null);
        } else {
            return Result.build(null, ResultCodeEnum.SYSTEM_ERROR);
        }
    }
}




